package com.spic.business.registrationform.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import com.spic.business.registrationform.domain.*;
import com.spic.business.registrationform.service.*;
import com.spic.common.core.exception.ServiceException;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.StringUtils;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.system.api.RemoteFileService;
import org.apache.poi.hssf.usermodel.HSSFPictureData;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spic.business.registrationform.mapper.ArchiveStaffRecordMapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 一人一档 (南露天煤矿从业人员登记)Service业务层处理
 *
 * @author spic
 * @date 2022-07-20
 */
@Service
public class ArchiveStaffRecordServiceImpl implements IArchiveStaffRecordService {
    @Autowired
    private ArchiveStaffRecordMapper archiveStaffRecordMapper;
    @Autowired
    private IArchiveStaffWorkExperienceService archiveStaffWorkExperienceService;
    @Autowired
    private IArchiveStaffPositionChangeService archiveStaffPositionChangeService;
    @Autowired
    private IArchiveStaffPromotionRecordService archiveStaffPromotionRecordService;
    @Autowired
    private IArchiveStaffDisciplinaryRecordsService archiveStaffDisciplinaryRecordsService;
    @Autowired
    private IArchiveStaffPostholdersRecordsService archiveStaffPostholdersRecordsService;
    @Autowired
    private IArchiveStaffJobholderRecordsService archiveStaffJobholderRecordsService;
    @Autowired
    private RemoteFileService remoteFileService;

    /**
     * 查询一人一档 (南露天煤矿从业人员登记)
     *
     * @param staffArchiveId 一人一档 (南露天煤矿从业人员登记)主键
     * @return 一人一档 (南露天煤矿从业人员登记)
     */
    @Override
    public ArchiveStaffRecord selectArchiveStaffRecordByStaffArchiveId(String staffArchiveId) {
        return archiveStaffRecordMapper.selectArchiveStaffRecordByStaffArchiveId(staffArchiveId);
    }

    /**
     * 查询一人一档 (南露天煤矿从业人员登记)列表
     *
     * @param archiveStaffRecord 一人一档 (南露天煤矿从业人员登记)
     * @return 一人一档 (南露天煤矿从业人员登记)
     */
    @Override
    public List<ArchiveStaffRecord> selectArchiveStaffRecordList(ArchiveStaffRecord archiveStaffRecord) {
        return archiveStaffRecordMapper.selectArchiveStaffRecordList(archiveStaffRecord);
    }

    /**
     * 新增一人一档 (南露天煤矿从业人员登记)
     *
     * @param archiveStaffRecord 一人一档 (南露天煤矿从业人员登记)
     * @return 结果
     */
    @Override
    public int insertArchiveStaffRecord(ArchiveStaffRecord archiveStaffRecord) {
        archiveStaffRecord.setCreateTime(DateUtils.getNowDate());
        return archiveStaffRecordMapper.insertArchiveStaffRecord(archiveStaffRecord);
    }

    /**
     * 修改一人一档 (南露天煤矿从业人员登记)
     *
     * @param staffRecordDTO 一人一档 (南露天煤矿从业人员登记)
     * @return 结果
     */
    @Override
    @Transactional
    public int updateArchiveStaffRecord(StaffRecordDTO staffRecordDTO) {
        ArchiveStaffRecord staffRecord = staffRecordDTO.getStaffRecord();
        if(StringUtils.isEmpty(staffRecord.getStaffArchiveId())) {
            throw new ServiceException("员工档案id为空");
        }
        String staffArchiveId = staffRecord.getStaffArchiveId();
        String[] staffArchiveIds = {staffArchiveId};
        archiveStaffRecordMapper.updateArchiveStaffRecord(staffRecord);

        archiveStaffWorkExperienceService.deleteArchiveStaffWorkExperienceByStaffArchiveIds(staffArchiveIds);
        archiveStaffPositionChangeService.deleteArchiveStaffPositionChangeByStaffArchiveIds(staffArchiveIds);
        archiveStaffPromotionRecordService.deleteArchiveStaffPromotionRecordByStaffArchiveIds(staffArchiveIds);
        archiveStaffDisciplinaryRecordsService.deleteArchiveStaffDisciplinaryRecordsByStaffArchiveIds(staffArchiveIds);
        archiveStaffPostholdersRecordsService.deleteArchiveStaffPostholdersRecordsByStaffArchiveIds(staffArchiveIds);
        archiveStaffJobholderRecordsService.deleteArchiveStaffJobholderRecordsByStaffArchiveIds(staffArchiveIds);

        List<ArchiveStaffWorkExperience> staffWorkExperience = staffRecordDTO.getStaffWorkExperience();
        if(staffWorkExperience != null && staffWorkExperience.size() > 0) {
            for(ArchiveStaffWorkExperience experience : staffWorkExperience) {
                archiveStaffWorkExperienceService.insertArchiveStaffWorkExperience(experience);
            }
        }
        List<ArchiveStaffPositionChange> staffPositionChange = staffRecordDTO.getStaffPositionChange();
        if(staffPositionChange != null && staffPositionChange.size() > 0) {
            for (ArchiveStaffPositionChange positionChange : staffPositionChange) {
                archiveStaffPositionChangeService.insertArchiveStaffPositionChange(positionChange);
            }
        }
        List<ArchiveStaffPromotionRecord> staffPromotionRecord = staffRecordDTO.getStaffPromotionRecord();
        if(staffPromotionRecord != null && staffPromotionRecord.size() > 0) {
            for (ArchiveStaffPromotionRecord promotionRecord : staffPromotionRecord) {
                archiveStaffPromotionRecordService.insertArchiveStaffPromotionRecord(promotionRecord);
            }
        }
        List<ArchiveStaffDisciplinaryRecords> staffDisciplinaryRecords = staffRecordDTO.getStaffDisciplinaryRecords();
        if(staffWorkExperience != null && staffWorkExperience.size() > 0) {
            for(ArchiveStaffDisciplinaryRecords disciplinaryRecords : staffDisciplinaryRecords) {
                archiveStaffDisciplinaryRecordsService.insertArchiveStaffDisciplinaryRecords(disciplinaryRecords);
            }
        }
        List<ArchiveStaffPostholdersRecords> staffPostholdersRecords = staffRecordDTO.getStaffPostholdersRecords();
        if(staffWorkExperience != null && staffWorkExperience.size() > 0) {
            for(ArchiveStaffPostholdersRecords postholdersRecords : staffPostholdersRecords) {
                archiveStaffPostholdersRecordsService.insertArchiveStaffPostholdersRecords(postholdersRecords);
            }
        }
        List<ArchiveStaffJobholderRecords> staffJobholderRecords = staffRecordDTO.getStaffJobholderRecords();
        if(staffWorkExperience != null && staffWorkExperience.size() > 0) {
            for(ArchiveStaffJobholderRecords jobholderRecords : staffJobholderRecords) {
                archiveStaffJobholderRecordsService.insertArchiveStaffJobholderRecords(jobholderRecords);
            }
        }

        return 1;
    }

    /**
     * 批量删除一人一档 (南露天煤矿从业人员登记)
     *
     * @param staffArchiveIds 需要删除的一人一档 (南露天煤矿从业人员登记)主键
     * @return 结果
     */
    @Override
    public int deleteArchiveStaffRecordByStaffArchiveIds(String[] staffArchiveIds) {
        return archiveStaffRecordMapper.deleteArchiveStaffRecordByStaffArchiveIds(staffArchiveIds);
    }

    /**
     * 删除一人一档 (南露天煤矿从业人员登记)信息
     *
     * @param staffArchiveId 一人一档 (南露天煤矿从业人员登记)主键
     * @return 结果
     */
    @Override
    public int deleteArchiveStaffRecordByStaffArchiveId(String staffArchiveId) {
        return archiveStaffRecordMapper.deleteArchiveStaffRecordByStaffArchiveId(staffArchiveId);
    }

    @Override
    @Transactional
    public void importData(InputStream inputStream) throws Exception {
        Workbook wookbook = new HSSFWorkbook(inputStream);
        inputStream.close();
        HSSFSheet sheet = (HSSFSheet) wookbook.getSheetAt(0);
        Map<String, HSSFPictureData> pictures = ExcelUtil.getPictures(sheet);

        ExcelUtil<ArchiveStaffRecord> util = new ExcelUtil(ArchiveStaffRecord.class);
        List<ArchiveStaffRecord> list = util.myImportExcel("基本信息", wookbook);
        importDataAtom(list, pictures);
        //工作经历
        ExcelUtil<ArchiveStaffWorkExperience> util1 = new ExcelUtil(ArchiveStaffWorkExperience.class);
        List<ArchiveStaffWorkExperience> workExperiences = util1.myImportExcel("工作经历", wookbook);
        archiveStaffWorkExperienceService.importDataAtom(workExperiences);

        //岗位变动情况
        ExcelUtil<ArchiveStaffPositionChange> util2 = new ExcelUtil(ArchiveStaffPositionChange.class);
        List<ArchiveStaffPositionChange> staffPositionChanges = util2.myImportExcel("岗位变动情况", wookbook);
        staffPositionChanges.stream().forEach(item -> {
            archiveStaffPositionChangeService.insertArchiveStaffPositionChange(item);
        });

        //职称/技能等级晋升情况记录
        ExcelUtil<ArchiveStaffPromotionRecord> util3 = new ExcelUtil(ArchiveStaffPromotionRecord.class);
        List<ArchiveStaffPromotionRecord> staffPromotionRecords = util3.myImportExcel("职称或技能等级晋升情况记录", wookbook);
        staffPromotionRecords.stream().forEach(item -> {
            archiveStaffPromotionRecordService.insertArchiveStaffPromotionRecord(item);
        });

        //安全生产奖惩记录
        ExcelUtil<ArchiveStaffDisciplinaryRecords> util4 = new ExcelUtil(ArchiveStaffDisciplinaryRecords.class);
        List<ArchiveStaffDisciplinaryRecords> staffDisciplinaryRecords = util4.myImportExcel("安全生产奖惩记录", wookbook);
        staffDisciplinaryRecords.stream().forEach(item -> {
            archiveStaffDisciplinaryRecordsService.insertArchiveStaffDisciplinaryRecords(item);
        });

        //南露天煤矿三项岗位人员安全培训、考核记录
        ExcelUtil<ArchiveStaffPostholdersRecords> util5 = new ExcelUtil(ArchiveStaffPostholdersRecords.class);
        List<ArchiveStaffPostholdersRecords> staffPostholdersRecordss = util5.myImportExcel("三项岗位人员安全培训、考核记录", wookbook);
        staffPostholdersRecordss.stream().forEach(item -> {
            archiveStaffPostholdersRecordsService.insertArchiveStaffPostholdersRecords(item);
        });

        //南露天煤矿从业人员安全培训、考核记录
        ExcelUtil<ArchiveStaffJobholderRecords> util6 = new ExcelUtil(ArchiveStaffJobholderRecords.class);
        List<ArchiveStaffJobholderRecords> staffJobholderRecords = util6.myImportExcel("从业人员安全培训、考核记录", wookbook);
        staffJobholderRecords.stream().forEach(item -> {
            archiveStaffJobholderRecordsService.insertArchiveStaffJobholderRecords(item);
        });
    }


    public void importDataAtom(List<ArchiveStaffRecord> list, Map<String, HSSFPictureData> pictures) throws IOException {
        if (list == null || list.size() == 0) {
            return;
        }

        for (int i = 0; i < list.size(); i++) {
            ArchiveStaffRecord staffRecord = list.get(i);
            if (pictures.containsKey(staffRecord.getStaffArchiveId())) {
                staffRecord.setPhoto(pictures.get(staffRecord.getStaffArchiveId()).getData());
            }
            insertArchiveStaffRecord(staffRecord);
        }
    }
}
